﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.Core;

namespace Pampa.MVC.Fluent.Crud.Groups
{
    public class DetailsGroupBuilder<TGroup, TEditor> : GroupBuilder<TGroup, TEditor>
    {
        private readonly DetailsGroup _detailsGroup;

        public DetailsGroupBuilder(EntityConfig<TEditor> entityConfig, DetailsGroup group) : base(entityConfig, group)
        {
            _detailsGroup = group;
        }

        public DetailsGroupBuilder<TGroup, TEditor> Title(string title)
        {
            _detailsGroup.Title = title;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddEditPermission(Func<TEditor, bool> action)
        {
            var guid = Guid.NewGuid().ToString();
            this.ParentEditor.SecurityManager.AddEditRule(guid, action);
            _detailsGroup.PermissionRequired = guid;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddViewPermission(Func<TEditor, bool> action)
        {
            var guid = Guid.NewGuid().ToString();
            this.ParentEditor.SecurityManager.AddViewRule(guid, action);
            _detailsGroup.PermissionRequired = guid;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddCustomPermission(string permision)
        {
            _detailsGroup.PermissionRequired = permision;
            return this;
        }

        /// <summary>
        /// Allows to override delete action.
        /// Action returns true if the entity repository delete action should be called after this.
        /// </summary>
        public void OnDelete(Expression<Func<TGroup, bool>> action)
        {
            EntityManager.OnDelete(action);
        }
    }
}
